#
#
import os, sys
if __name__ == '__main__':
	execfile(os.path.join(sys.path[0], 'framework.py'))

from Testing import ZopeTestCase
from testSurvey import SurveyTestCase
ZopeTestCase.installProduct('ArchSurvey')

question_types = ['LongAnswerQuestion',
			'ShortAnswerQuestion',
			'LabelQuestion',
			'PickOneQuestion',
			'PickManyQuestion',
			'YesNoQuestion',
			'NumericQuestion',
			'DateAndTimeQuestion',
			'ListQuestion']

test_string= "Test String"
test_string2 = "Test String 2"

class TestQuestions(SurveyTestCase):

	def testInstallQuestions(self):
		s = self.addSurvey()
		x = s.listQuestionTypes()
		for meta_type in question_types:
			self.failUnless(meta_type in x, "%s not registered" % meta_type)

	def testYesNoAnswer(self):
		q = self.addQuestion('YesNoQuestion')
		q.addAnswer(0,0)
		q.addAnswer(1,0)
		q.addAnswer(2,1)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,0),(1,0),(2,1)],"%s" % answers )
		q.addAnswer(1,1)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,0),(1,1),(2,1)],"%s" % answers )
		
	def testLongAnswer(self):
		q = self.addQuestion('LongAnswerQuestion')
		q.addAnswer(0,test_string)
		q.addAnswer(23,test_string2)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,test_string),(23,test_string2)], answers)
		q.addAnswer(23,test_string)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,test_string),(23,test_string)], answers)

	def testShortAnswer(self):
		q = self.addQuestion('ShortAnswerQuestion')
		q.addAnswer(0,test_string)
		q.addAnswer(23,test_string2)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,test_string),(23,test_string2)], answers)
		q.addAnswer(23,test_string)
		answers = list(q.getResults())
		self.failUnless(answers == [(0,test_string),(23,test_string)], answers)

	def testAddQuestions(self):
		s = self.addSurvey()
		for meta_type in question_types:
			s.addQuestion(meta_type)

class TestFunctionalQuestions(ZopeTestCase.Functional, SurveyTestCase):

	def testSurveyQuestions(self):
		s = self.addSurvey()
		s.addQuestion('YesNoQuestion')
		s.addQuestion('LongAnswerQuestion')
		self.login('manager')
		basic_auth = "manager:secret"
		resp = self.publish('/%s/survey_take' % s.absolute_url(1), basic=basic_auth)
		print resp.getOutput()
		self.failUnless(resp.getStatus() == 200, "Wrong status: %s" % resp.getStatus())
		
		
def test_suite():
	from unittest import TestSuite, makeSuite
	suite = TestSuite()
	suite.addTest(makeSuite(TestQuestions))
	suite.addTest(makeSuite(TestFunctionalQuestions))
	return suite

if __name__ == '__main__':
	framework()
	   